package com.wangp.mywrite.s_juc.concurrtent_tools;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author farling-wangp
 * @version 1.0
 * @date 2020/11/15 20:18
 */
public class ConcurrentHashMapTest implements Runnable {
    static ConcurrentHashMap<String, Integer> concurrentHashMap = new ConcurrentHashMap();

    public static void main(String[] args) throws InterruptedException {
        concurrentHashMap.put("hello", 0);
        Thread t1 = new Thread(new ConcurrentHashMapTest());
        Thread t2 = new Thread(new ConcurrentHashMapTest());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(concurrentHashMap);
    }

    @Override
    public void run() {
        for (int i = 0; i < 100000; i++) {

                Integer score = concurrentHashMap.get("hello");
//                concurrentHashMap.put("hello", score + 1);
//                boolean flag = concurrentHashMap.replace("hello", score, score + 1);
        concurrentHashMap.compute("hello",(k,v)-> v+1);
        }
    }
}
